package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.CheckTime;
import com.zhitong.money.entity.Salary;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.SalaryMapper;
import com.zhitong.money.services.api.ISalaryService;
import com.zhitong.money.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SalaryService implements ISalaryService {

    @Autowired
    private SalaryMapper mapper;

    @Override
    public List<Salary> findAllSalary() throws Exception {
        List<Salary> allSalary = mapper.findAllSalary();
        return allSalary;
    }

    @Override
    public Salary findSalaryById(String id) throws Exception {
        Salary salaryById = mapper.findSalaryById(id);
        return salaryById;
    }

    @Override
    public PageInfo findSalaryByPage(Map<String, Object> map) throws Exception {
        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;
        PageHelper.startPage(pageNum,pageSize);
        List<Salary> salaryByMapPage = mapper.findSalaryByMap(map);
        PageInfo p=new PageInfo(salaryByMapPage);
        return p;
    }

    @Override
    public List<Salary> findSalaryByMap(Map<String, Object> map) throws Exception {
        return null;
    }

    @Override
    public void addSalary(Salary salary) throws Exception {
        Salary salaryByTidAndTime = mapper.findSalaryByTidAndTime(salary.getTid(), DateUtil.dateToStr(salary.getDate(),DateUtil.FORMAT_DATE_CHECK));
        if (salaryByTidAndTime!=null){
            throw new RepeatException("该员工本月工资已生成!");
        }
        try {
            mapper.addSalary(salary);
        } catch (Exception e) {
            throw new AddException("addSalary:添加工资失败!"+e.getMessage());
        }
    }

    @Override
    public void updateSalary(Salary salary) throws Exception {
        try {
            mapper.updateSalary(salary);
        } catch (Exception e) {
            throw new UpdateException("updateSalary:更新工资失败"+e.getMessage());
        }
    }

    @Override
    public void delSalary(String id) throws Exception {
        try {
            mapper.delSalary(id);
        } catch (Exception e) {
            throw new DeleteException("delSalary:删除失败"+e.getMessage());
        }
    }

    @Override
    public void delSalaryByIds(List<String> ids) throws Exception {
        try {
            mapper.delSalaryByIds(ids);
        } catch (Exception e) {
            throw new DeleteException("delSalaryByIds:删除失败"+e.getMessage());
        }
    }

    @Override
    public void exportSalarys(Map<String, Object> map, HttpServletResponse response) throws Exception {
        //       找到数据
        String[] titles = {"员工编号","员工姓名","发放日期","基础工资","全勤奖","岗位补贴","其他补贴","加班(时)","加班工资",
        "应发工资","请假(天)","请假应扣","迟到(时)","迟到应扣","早退(时)","早退应扣","缺勤(天)","缺勤应扣","税率","应扣税费","应扣","实发"};
        List<Salary> salaryByMap = mapper.findSalaryByMap(map);
        HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheet = sheets.createSheet("工资");


        //设置标题样式
        HSSFCellStyle cellStyle = sheets.createCellStyle();
        //水平垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
        //字体
        HSSFFont font = sheets.createFont();
        font.setFontName("华文行楷");//设置字体名称
        font.setFontHeightInPoints((short)12);//设置字号
        font.setBold(true);
        cellStyle.setFont(font);

        //设置标题
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            //设置宽度
            sheet.setColumnWidth(i,15*256);
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(cellStyle);
        }
        //设置内容
        Salary salary = new Salary();

        for (int i = 0; i < salaryByMap.size(); i++) {
            row = sheet.createRow(i+1);
            salary=salaryByMap.get(i);
            row.createCell(0).setCellValue(salary.getTid());
            row.createCell(1).setCellValue(salary.getName());
            row.createCell(2).setCellValue(DateUtil.dateToStr(salary.getDate(),DateUtil.FORMAT_DATE_CHECK));
            row.createCell(3).setCellValue(salary.getBasicSalary());
            row.createCell(4).setCellValue(salary.getFullAttention());
            row.createCell(5).setCellValue(salary.getPositionSalary());
            row.createCell(6).setCellValue(salary.getSubsidy());
            row.createCell(7).setCellValue(salary.getOverTime());
            row.createCell(8).setCellValue(salary.getOverTimeSalary());
            row.createCell(9).setCellValue(salary.getTotalPay());
            row.createCell(10).setCellValue(salary.getLeaveTime());
            row.createCell(11).setCellValue(salary.getLeaveDeduction());
            row.createCell(12).setCellValue(salary.getLateTime());
            row.createCell(13).setCellValue(salary.getLateDeduction());
            row.createCell(14).setCellValue(salary.getEarlyTime());
            row.createCell(15).setCellValue(salary.getEarlyDeduction());
            row.createCell(16).setCellValue(salary.getAbsenceDay());
            row.createCell(17).setCellValue(salary.getOtherDeduction());
            row.createCell(18).setCellValue(salary.getTaxRate());
            row.createCell(19).setCellValue(salary.getTaxSalary());
            row.createCell(20).setCellValue(salary.getTotalDeduction());
            row.createCell(21).setCellValue(salary.getTotal());
        }
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + "check.xls");
        // 响应类型,编码
        response.setContentType("application/octet-stream;charset=UTF-8");

        ServletOutputStream outputStream = response.getOutputStream();
        sheets.write(outputStream);

        outputStream.flush();
        outputStream.close();

    }
}
